package com.gec.anan.driver.service.impl;

import com.alibaba.nacos.common.codec.Base64;
import com.gec.anan.driver.config.TencentCloudProperties;
import com.gec.anan.driver.service.CiService;
import com.gec.anan.model.vo.order.TextAuditingVo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ciModel.auditing.*;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CiServiceImpl implements CiService {

    @Autowired
    private TencentCloudProperties tencentCloudProperties;

    @Override
    public Boolean imageAuditing(String path) {
        //获取客户端
        COSClient client = this.getPrivateCOSClient();
        //腾讯云审核接口
        //1.创建任务请求对象
        ImageAuditingRequest request = new ImageAuditingRequest();
        //2.添加请求参数 参数详情请见 API 接口文档
        //2.1设置请求 bucket
        request.setBucketName(tencentCloudProperties.getBucketPrivate());
        //2.2设置审核策略 不传则为默认策略（预设）
        //request.setBizType("");
        //2.3设置 bucket 中的图片位置
        request.setObjectKey(path);
        //3.调用接口,获取任务响应对象
        ImageAuditingResponse response = client.imageAuditing(request);
        //用于返回该审核场景的审核结果，返回值：0：正常。1：确认为当前场景的违规内容。2：疑似为当前场景的违规内容。
        if (!response.getPornInfo().getHitFlag().equals("0")
                || !response.getAdsInfo().getHitFlag().equals("0")
                || !response.getTerroristInfo().getHitFlag().equals("0")
                || !response.getPoliticsInfo().getHitFlag().equals("0")
        ) {
            return false;
        }
        return true;
    }

    //审核文本内容
    @Override
    public TextAuditingVo textAuditing(String content) {
        TextAuditingVo textAuditingVo = new TextAuditingVo();
        //判断文本是否为空
        if(!StringUtils.hasText(content)){
            textAuditingVo.setResult("0");
            return textAuditingVo;
        }
        //获取客户端对象
        COSClient client = this.getPrivateCOSClient();
        //1.创建任务请求对象
        TextAuditingRequest request = new TextAuditingRequest();
        //2.添加请求参数 参数详情请见 API 接口文档
        request.setBucketName(tencentCloudProperties.getBucketPrivate());
        //2.1.1设置请求内容,文本内容的Base64编码
        byte[] bytes = Base64.encodeBase64(content.getBytes());
        String contentBase = new String(bytes);
        request.getInput().setContent(contentBase);
        request.getConf().setDetectType("all");
        //或是cos中的设置对象地址 不可同时设置
        //3.调用接口,获取任务响应对象
        TextAuditingResponse response = client.createAuditingTextJobs(request);
        //是否成功
        if("Success".equals(response.getJobsDetail().getState())){
            String result = response.getJobsDetail().getResult();
            //获取违规词
            StringBuffer keysWord = new StringBuffer();
            List<SectionInfo> sectionList = response.getJobsDetail().getSectionList();
            for (SectionInfo section:sectionList) {
                String keywords = section.getPornInfo().getKeywords();
                if(StringUtils.hasText(keywords)){
                    keysWord.append(keywords).append(",");
                }
                String  adsInfo = section.getAdsInfo().getKeywords();
                if(StringUtils.hasText(adsInfo)){
                    keysWord.append(adsInfo).append(",");
                }
                String keywords1 = section.getIllegalInfo().getKeywords();
                if(StringUtils.hasText(keywords1)){
                    keysWord.append(keywords1).append(",");
                }
                String keywords2 = section.getAbuseInfo().getKeywords();
                if(StringUtils.hasText(keywords2)){
                    keysWord.append(keywords2).append(",");
                }
                textAuditingVo.setResult(result);
                textAuditingVo.setKeywords(keywords);
            }
        }
        return textAuditingVo;
    }

    //腾讯云客户端
    private COSClient getPrivateCOSClient() {
        COSCredentials cred = new BasicCOSCredentials(tencentCloudProperties.getSecretId(), tencentCloudProperties.getSecretKey());
        ClientConfig clientConfig = new ClientConfig(new Region(tencentCloudProperties.getRegion()));
        clientConfig.setHttpProtocol(HttpProtocol.https);
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }
}
